$(function () {
    //获取url
    var url = localStorage.getItem("ajaxUrl");
    var pass = 1;

    //点击返回箭头 清除 缓存 打开 storekeeper.html
    $(".head .goBack").on("click", function () {
        window.location.href = "storekeeperRope.html";
    });

    //点击 刷新 按钮  重新加载页面
    $(".page .head").on("click", "span.icon-refresh-1-copy", function () {
        window.location.href = "returnorderRope.html";
    });

    //获取发货单列表
    getReturnorders();

    function getReturnorders() {
        //加载提示符 显示
        $(".content .list-block ul#work_list .infinite-scroll-preloader").show();
        $.ajax({
            headers: {authorization: "Bearer " + localStorage.getItem("jwt")},
            type: "post",
            url: url + '/app/productsalereturn/getProductRopesalereturnorders',
            dataType: 'json',
            json: 'callback',
            cache: false,
            timeout: 60000,
            success: function (res) {
                if (res.success) {
                    var data = res.result;
                    //清空页面中的单子
                    $("#work_list").find("li").remove();
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    //console.log(data);
                    var data_html = '';
                    for (var i = 0; i < data.length; i++) {
                        data_html += '<li>' +
                            '<p class="li_title">退货单号：<span class="sp work_returnCode">' + data[i].number + '</span><span class="icon iconfont icon-right pull-right"></span></p>' +
                            '<div class="row no-gutter li_content"><div class="col-100">' +
                            '<p class="p_none"><span>id:</span><span class="sp goods_id">' + data[i]._id + '</span></p>' +
                            '<p class="p_none"><span>houseCode:</span><span class="sp goods_houseCode">' + data[i].warehouseCode + '</span></p>' +
                            '<div class="details">' +
                            '<p><span>订单号:</span><span class="sp goods_workplanNumber">' + (data[i].saleorderNumber) + '</span></p>' +
                            '<p><span>排产单号:</span><span class="sp goods_workplanNumber">' + (data[i].relationCompanyWorkplanNumber ? data[i].relationCompanyWorkplanNumber : data[i].workplanNumber) + '</span></p>' +
                            '<p><span>客户:</span><span class="sp goods_customerShort">' + (data[i].customerNameShort ? data[i].customerNameShort : "") + '</span></p>' +
                            '<p><span>仓库:</span><span class="sp goods_houseName">' + data[i].warehouseName + '</span></p>' +
                            '<p><span>存货名称:</span><span class="sp goods_productName">' + data[i].inventoryName + '</span></p>' +
                            '<p><span>规格:</span><span class="sp goods_specification">' + data[i].inventorySpecification + '</span>' +
                            '<span>计划退货:</span><span class="sp goods_expectReturnWeight">' + data[i].planQuantity + '</span>T</p>' +
                            '<p><span>制单日期:</span><span class="sp goods_createDate">' + data[i].createDate + '</span></p>';
                        if (data[i].comment) {
                            data_html += '<p><span>备注:</span><span class="sp comment" style="display: inline;">' + data[i].comment + '</span></p>';
                        }
                        data_html += '</div></div></div></li>';
                    }
                    $("#work_list").append(data_html);
                    //改变 共计 数
                    $(".content div.ul_foot span.sp_js").html($("#work_list li").length);
                } else {
                    //隐藏提示符 显示
                    $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                    $alert(res.message);
                }
            },
            error: function (error) {
                //隐藏提示符 显示
                $(".content .list-block ul#work_list .infinite-scroll-preloader").hide();
                if (error.status == 0) {
                    //loading_hide();
                    $alert("请求超时，请重新操作！");
                } else {
                    //loading_hide();
                    $alert(error.responseText);
                }
            }
        });
    }

    //点击 发货单箭头
    $(".content #work_list").on("click", "li .li_title", function () {
        var id = $(this).parent().find("span.goods_id").html();
        var returnCode = $(this).parent().find("span.work_returnCode").html();
        var workplanNumber = $(this).parent().find("span.goods_workplanNumber").html();
        var houseCode = $(this).parent().find("span.goods_houseCode").html();
        var houseName = $(this).parent().find("span.goods_houseName").html();
        var productName = $(this).parent().find("span.goods_productName").html();
        var specification = $(this).parent().find("span.goods_specification").html();
        var expectReturnWeight = $(this).parent().find("span.goods_expectReturnWeight").html();
        var work = {
            id: id,
            returnCode: returnCode,
            workplanNumber: workplanNumber,
            houseCode: houseCode,
            houseName: houseName,
            productName: productName,
            specification: specification,
            expectReturnWeight: expectReturnWeight
        };
        localStorage.setItem("work", JSON.stringify(work));
        window.location.href = "returnorderRope_one.html";
    });

});